﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Collections.ObjectModel;
using System.Globalization;
using Microsoft.Practices.ObjectBuilder;
using Microsoft.Practices.CompositeWeb;
using WCSFContrib.ECommerceCatalog.BusinessEntities;

namespace WCSFContrib.ECommerceCatalog.User.Views
{
    public class CartPresenter : Presenter<ICartView>
    {
        private IUserController _controller;
        private string _userName;

        public string UserName
        {
            set { _userName = value; }
        }

        public CartPresenter([CreateNew] IUserController controller)
        {
            this._controller = controller;
            this._controller.StartUserPageFlow();
            this._controller.CurrentPage = "Cart";
        }

        public override void OnViewInitialized()
        {
            LoadCartItemsForCurrentUser();
        }

        public void OnProductDetailsRequested(int productId)
        {
            Item selectedProduct = _controller.GetProduct(productId);
            View.ShowProductDetails(selectedProduct);
        }

        public void OnDeleteProduct(string userName, int productId, int quantity)
        {
            _controller.RemoveItemFromCart(userName, productId, quantity);
            LoadCartItemsForCurrentUser();
        }

        public void OnCheckoutOrder()
        {
            _controller.GoCheckoutPage();   
        }

        private void LoadCartItemsForCurrentUser()
        {
            string userName = _userName;
            Cart cart = _controller.GetCartByUserName(userName);

            View.ShowCart(cart);
        }
    }
}




